home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17379 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: news.sprintlink.net!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ beginner quesion on data member access.
  5. Date: Mon, 15 Apr 1996 13:19:38 -0400
  6. Organization: Datalytics, Inc
  7. Message-ID: <3172852A.7A76@datalytics.com>
  8. References: <4kgb76$r3s@HOPPER.ACM.ORG> <316E7140.C5D@platinum.com>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Michael Scott wrote:
  16. > Ken Varn wrote:
  17. > >
  18. > >      I have been struggling with the proper way to declare and access data
  19. > > members in my classes.  I was once told that a class should not allow any
  20. > > data members to be public and that they should only be accessed via member
  21. > > functions.  Why?  I know the reasons for declaring private data, but what do
  22. > > I gain if all I am doing is providing a member function to get the private
  23. > > data or set the private data.  i.e. why call a getData() function that
  24. > > basically just returns the private data member as opposed to just declaring
  25. > > the pviate data member as public.
  26. > Classes encapsulate the behavior of an object.  Data members
  27. > represent the "state" of an object.  Data access functions
  28. > provide access to the internal state of an object while allowing
  29. > the implementation of that state to vary without necessarily
  30. > changing the interface to the object.
  31. > Pragmatically speaking though, there are classes and
  32. > applications where the trade-offs between encapsulation overhead
  33. > and direct public member access favor the public data.
  34. > Balancing the trade-offs is what makes programming an art rather
  35. > than a strict science.
  36.  
  37. More specifically, exposing a dm as public means you can never 
  38. change the data type without risking breaking code dependent 
  39. upon it.  You also make that dm available for class 
  40. users to change without your class being able to 
  41. control it.
  42.  
  43. Consider, for example, a simple string class.  If you expose the 
  44. length dm, then you allow the class user to read and write it.  
  45. What's more, if you decide that you want to add reference 
  46. counting to your string class, the length actually comes from 
  47. the representation.  Your string class is just a handle to the 
  48. shared representation.  Thus, you would have to duplicate the 
  49. length in your string class' dm just so those class users 
  50. dependent upon a dm to get the length can still work.
  51.  
  52. If, on the other hand, the length came from a member function 
  53. call, you could change that mf to get the length from the 
  54. representation.  In other words, the class users wouldn't know 
  55. that your implementation changed.  That is the heart of 
  56. encapsulation.
  57.  
  58. As for performance, if that is a concern, an inline mf that 
  59. returns the value of a dm is not slower than reading a public 
  60. dm.  Of course this raises implementation exposure issues for 
  61. libraries which you may not want to incur (the subject of a 
  62. recent thread).
  63.  
  64. -- 
  65. Robert Stewart        | My opinions are usually my own.
  66. Datalytics, Inc.    | stew@datalytics.com
  67.